package com.example.udpmulticastdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import com.example.udpmulticastdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private val mBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private var mUdpUnicastReceiverAsyncTask: UdpUnicastReceiverAsyncTask? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        initView()
    }


    private fun initView() {
        mBinding.sendMsgBtn.setOnClickListener {
            mBinding.sendMsgBtn.isEnabled = false
            mBinding.receiveMsgBtn.isEnabled = false
            mBinding.sendUdpUnicastMsgBtn.isEnabled = false
            mBinding.receiveUnicastMsgBtn.isEnabled = false
            mBinding.udpUnicastIpEt.isEnabled = false
            mBinding.udpUnicastPortEt.isEnabled = false
            mBinding.cancelBtn.visibility = View.VISIBLE
            //todo 开启UDP组播发送端

        }
        mBinding.receiveMsgBtn.setOnClickListener {
            mBinding.sendMsgBtn.isEnabled = false
            mBinding.receiveMsgBtn.isEnabled = false
            mBinding.sendUdpUnicastMsgBtn.isEnabled = false
            mBinding.receiveUnicastMsgBtn.isEnabled = false
            mBinding.udpUnicastIpEt.isEnabled = false
            mBinding.udpUnicastPortEt.isEnabled = false
            mBinding.cancelBtn.visibility = View.VISIBLE
            MediaUDPClient.getInstance().connServer()
        }
        mBinding.sendUdpUnicastMsgBtn.setOnClickListener {
            mBinding.sendMsgBtn.isEnabled = false
            mBinding.receiveMsgBtn.isEnabled = false
            mBinding.sendUdpUnicastMsgBtn.isEnabled = false
            mBinding.receiveUnicastMsgBtn.isEnabled = false
            mBinding.udpUnicastIpEt.isEnabled = false
            mBinding.udpUnicastPortEt.isEnabled = false
            mBinding.cancelBtn.visibility = View.VISIBLE
            //开启UDP单播发送
            UDPSingleCast.start(
                mBinding.udpUnicastIpEt.text.trim().toString(),
                mBinding.udpUnicastPortEt.text.toString().toInt()
            )

        }
        mBinding.receiveUnicastMsgBtn.setOnClickListener {
            mBinding.sendMsgBtn.isEnabled = false
            mBinding.receiveMsgBtn.isEnabled = false
            mBinding.sendUdpUnicastMsgBtn.isEnabled = false
            mBinding.receiveUnicastMsgBtn.isEnabled = false
            mBinding.udpUnicastIpEt.isEnabled = false
            mBinding.udpUnicastPortEt.isEnabled = false
            mBinding.cancelBtn.visibility = View.VISIBLE
            //开启UDP单播接收
            mUdpUnicastReceiverAsyncTask = UdpUnicastReceiverAsyncTask(8900)
            mUdpUnicastReceiverAsyncTask?.execute()
        }
        mBinding.cancelBtn.setOnClickListener {
            //停止UDP组播接收
            MediaUDPClient.getInstance().onStop()
            // TODO: 停止UDP组播的发送


            // TODO: 停止UDP单播的发送
            UDPSingleCast.stop()
            // TODO: 停止UDP单播的接收
            mUdpUnicastReceiverAsyncTask?.cancel(true)
            mUdpUnicastReceiverAsyncTask?.stop()

            mBinding.sendMsgBtn.isEnabled = true
            mBinding.receiveMsgBtn.isEnabled = true
            mBinding.sendUdpUnicastMsgBtn.isEnabled = true
            mBinding.receiveUnicastMsgBtn.isEnabled = true
            mBinding.udpUnicastIpEt.isEnabled = true
            mBinding.udpUnicastPortEt.isEnabled = true
            mBinding.cancelBtn.visibility = View.GONE
        }
    }

    override fun onDestroy() {
        MediaUDPClient.getInstance().onStop()
        super.onDestroy()
    }
}
